package fr.web325.servlet.api.index;

import fr.web325.bean.Json;
import fr.web325.dao.ClientDao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@WebServlet("/api/search")
public class Search extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String content = req.getParameter("content");
        try {
            List<Map<String,Object>> searchList =  new ClientDao().searchByContent(content);
            if(searchList.isEmpty()){
                resp.getWriter().print(new Json(204,null,"未查找到该信息!").getRespJson());
            }else{
                JSONArray searchJson = JSONArray.fromObject(searchList);
                resp.getWriter().print(new Json(200,searchJson,"查找书籍成功!").getRespJson());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
